<template>
    <Card style="height:900px">
        <Form ref="form" :model="form" :rules="rules" :label-width="500">
            <FormItem label="生效时间" prop="startTime">
                <!--设置 :disabled="type==1" -->
                <Date-picker type="datetime" :disabled="type==2" :value="form.startTime" v-model="form.startTime" :readonly='true'>
                </Date-picker>
            </FormItem>
            <FormItem label="预约面签时间" prop="timeId">
                <i-select  size="large"  v-model="form.timeId"  placeholder="--请选择--" style="width:250px">
                    <i-option  v-for="item in facesigStartTimeList" :value="item.value" :key="item.value">{{ item.label}}</i-option>
                </i-select>
            </FormItem>
            <FormItem label="指定面签人员" prop="faceJobId">
                <i-select v-model="form.faceJobId" :model.sync="model2" size="large" placeholder="--请选择--" style="width:250px">
                    <i-option  v-for="item in faceJobIdList" :value="item.value" :key="item.value">{{ item.label }}</i-option>
                </i-select>
            </FormItem>
            <FormItem label="客户姓名" prop="custName">
               <Input v-model="form.custName" placeholder="" style="width: 250px">
               </Input>
            </FormItem>
            <FormItem label="客户证件号码" prop="custIdcard">
               <Input v-model="form.custIdcard" placeholder="" style="width: 250px">
               </Input>
            </FormItem>
             <FormItem label="客户手机号码" prop="custMobile">
               <Input v-model="form.custMobile" placeholder="" style="width: 250px">
               </Input>
            </FormItem>
            <FormItem v-if="type==2 || type==1">
                <Button type="primary" @click="handleSubmit">确定</Button>
                <Button type="default" style="margin-left: 8px" @click="handleGoBack">返回</Button>
            </FormItem>
            <FormItem v-if="type==0">
                <Button type="primary" @click="handleGo(1)">编辑</Button>
                <Button type="default" style="margin-left: 8px" @click="handleGoBack">返回</Button>
            </FormItem>
        </Form>
    </Card>
</template>

<script>
export default {
  data () {
    return {
      baseUrl: this.$appContext.marvels_core_approve,
      $ajax: null,
      faceJobIdList: [],
      facesigStartTimeList: [ ],
      model1: '',
      model2: '',
      form: {
        facesigTime: null,
        startTime: null,
        endTime: null,
        faceJobId: null,
        custName: null,
        custIdcard: null,
        custMobile: null,
        status: null,
        description: null,
        createdDate: null,
        updatedDate: null,
        createdBy: null,
        updatedBy: null,
        timeId: null
      },
      rules: {
        custName: [
          { type: 'string', required: true, message: '客户姓名不能为空', trigger: 'blur' }
        ],
        custIdcard: [
          { required: true, message: '客户证件号码不能为空', trigger: 'blur' },
          { pattern: this.$util.regExpIdCardNo, message: '客户证件号码格式不正确', trigger: 'blur' }
        ],
        custMobile: [
          { required: true, message: '客户手机号码不能为空', trigger: 'blur' },
          { pattern: this.$util.regExpMobile, message: '客户手机号码格式不正确', trigger: 'blur' }
        ],
        timeId: [
          { required: true, message: '预约面签时间不能为空', trigger: 'blur' }
        ]
      },
      copyRole: {}
    }
  },
  methods: {
    handleSubmit () {
      var vm = this
      this.$refs.form.validate(valid => {
        if (valid) {
          var config = { title: '提示' }
          if (this.type === 1) {
            config.content = '是否确认修改?'
            config.onOk = vm.updateFacesigTask
          } else if (this.type === 2) {
            config.content = '是否确认新建?'
            config.onOk = vm.addFacesigTask
          }
          vm.$Modal.confirm(config)
        }
      })
    },
    // 新建面签任务
    addFacesigTask: function () {
      var params = this.form
      this.$ajax.post(this.baseUrl + '/approve/spmq/service/addFacesigTask', params).then(re => {
        var data = re.data
        var code = data.code
        var message = data.message
        if (code !== 'success') {
          this.$Message.warning(data.data)
          return
        } else {
          this.$Message.success(message)
        }
        this.handleGoBack()
      }).catch(er => {

      })
    },
    handleGoBack: function () {
      this.$router.back()
    },
    updateFacesigTask: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          var params = this.form
          this.$ajax.post(this.baseUrl + 'system/pmsn/service/', params).then(re => {
            var data = re.data
            var code = data.code
            var message = data.message
            if (code !== 'success') {
              this.$Message.warning(message)
              return
            } else {
              this.$Message.warning(success)
            }
            this.handleGoBack()
          }).catch(er => {

          })
        }
      })
    },
    initpage: function () {
      if (this.type === 1) { // 编辑
        this.renderForm()
      }
    },
    // 获取所有用户
    getUserList: function () {
      this.$ajax.get(this.baseUrl + 'approve/spmq/service/getUserList').then(re => {
        var data = re.data
        var code = data.code
        if (code !== 'success') {
          this.$Message.warning('获取面签人员失败')
          return
        }
        // 面签员下拉框
        var vm = this
        var faceJobIdlist = data.data
        for (var i = 0; i < faceJobIdlist.length; i++) {
          vm.faceJobIdList.push({
            value: faceJobIdlist[i].userCode,
            label: faceJobIdlist[i].userNamep
          })
        }
      }).catch(er => {

      })
    },
    // 获取面签时间
    getfacesigStartTime: function () {
      this.$ajax.get(this.baseUrl + 'approve/mqcspz/service/getFacesigStartTimeAndendTime').then(re => {
        var data = re.data
        var code = data.code
        if (code !== 'success') {
          this.$Message.warning('获取预约面签时间失败')
          return
        }
        var vm = this
        var facesigStartTimeList = data.data.list
        for (var j = 0; j < facesigStartTimeList.length; j++) {
          vm.facesigStartTimeList.push({
            'value': facesigStartTimeList[j].facesigId + '',
            'label': facesigStartTimeList[j].startTime + '-' + facesigStartTimeList[j].endTime
          })
        }
      }).catch(er => {

      })
    }
  },
  computed: {
    type () { // 0 查看 1编辑 2新增
      return parseInt(this.$route.query.type)
    }
  },
  created () {
    this.$ajax = this.$util.ajax()
    this.initpage()
  },
  mounted () {
    var date = new Date()
    this.form.startTime = this.$util.formatTime(date.getTime())
    this.getUserList()
    this.getfacesigStartTime()
  }

}
</script>
